10. Exercise: Cancel the Notification
L1 A09 SC Cancel The Notificaiton
Android Developer Documentation
Exercise
- Open
NotificationsUtil.kt
- Add an extension function on
NotificationManagerwhich callscancelAll.
// NotificationUtils.kt
// TODO: Step 1.14 Cancel all notifications
/**
* Cancels all notifications.
*
*/
fun NotificationManager.cancelNotifications() {
cancelAll()
}
- Now open
EggTimerViewModel.ktand find thestartTimerfunction.
- Get an instance of the
NotificationManagerfrom the system and callcancelNotifications()extension function.
//TODO Step 1.15 call cancel notification
val notificationManager =
ContextCompat.getSystemService(
app,
NotificationManager::class.java
) as NotificationManager
notificationManager.cancelNotifications()
- Run the app and start the timer. After you see the notification, start the timer again and observe how our app automatically deleted the previous notification from the status bar.